package com.lanvideo.controller;

import com.lanvideo.service.UserService;
import com.lanvideo.service.VideoService;
import com.lanvideo.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统管理控制器
 */
@RestController
@RequestMapping("/api/admin/system")
public class SystemController {

    @Autowired
    private UserService userService;

    @Autowired
    private VideoService videoService;

    @Autowired
    private ExamService examService;

    /**
     * 获取系统统计信息
     */
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getSystemStats(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer adminId = (Integer) session.getAttribute("userId");
            if (adminId == null || userService.getUserById(adminId).getRole() != 0) {
                result.put("success", false);
                result.put("message", "只有管理员可以查看系统统计");
                return ResponseEntity.ok(result);
            }
            
            Map<String, Object> stats = new HashMap<>();
            stats.put("totalUsers", userService.getAllUsers().size());
            stats.put("totalVideos", videoService.getAllVideos().size());
            stats.put("totalExams", examService.getAllExams().size());
            stats.put("storageUsed", 0); // 暂时设为0，可以后续实现
            
            result.put("success", true);
            result.put("stats", stats);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统错误：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 检查数据库状态
     */
    @GetMapping("/db-status")
    public ResponseEntity<Map<String, Object>> checkDatabaseStatus(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer adminId = (Integer) session.getAttribute("userId");
            if (adminId == null || userService.getUserById(adminId).getRole() != 0) {
                result.put("success", false);
                result.put("message", "只有管理员可以检查数据库状态");
                return ResponseEntity.ok(result);
            }
            
            // 简单的数据库连接测试
            userService.getAllUsers();
            
            result.put("success", true);
            result.put("message", "数据库连接正常");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "数据库连接异常：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 数据备份
     */
    @PostMapping("/backup")
    public ResponseEntity<Map<String, Object>> backupData(@RequestBody Map<String, Object> request, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer adminId = (Integer) session.getAttribute("userId");
            if (adminId == null || userService.getUserById(adminId).getRole() != 0) {
                result.put("success", false);
                result.put("message", "只有管理员可以执行数据备份");
                return ResponseEntity.ok(result);
            }
            
            String backupType = (String) request.get("type");
            String description = (String) request.get("description");
            
            // 这里可以实现实际的备份逻辑
            // 暂时返回成功
            result.put("success", true);
            result.put("message", "备份任务已启动");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统错误：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 清理缓存
     */
    @PostMapping("/clear-cache")
    public ResponseEntity<Map<String, Object>> clearCache(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer adminId = (Integer) session.getAttribute("userId");
            if (adminId == null || userService.getUserById(adminId).getRole() != 0) {
                result.put("success", false);
                result.put("message", "只有管理员可以清理缓存");
                return ResponseEntity.ok(result);
            }
            
            // 这里可以实现实际的缓存清理逻辑
            // 暂时返回成功
            result.put("success", true);
            result.put("message", "缓存清理成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "系统错误：" + e.getMessage());
        }
        return ResponseEntity.ok(result);
    }
}
